// miniprogram/pages/profile-bloghistory/profile-bloghistory.js
const db = wx.cloud.database();
const MAX_LIMIT = 10;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    blogList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getListCloudFn();
    // this.getListMinaFn()
  },
  /**
   * 使用云服务
   */
  getListCloudFn() {
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'blog',
      data: {
        $url: "getMyBlog",
        start: this.data.blogList.length,
        count: MAX_LIMIT
      }
    }).then(res => {
      this.setData({
        blogList: this.data.blogList.concat(res.result)
      })
      wx.hideLoading();

      console.log(res)
    })
  },
  /**
   * 使用小程序
   */
  getListMinaFn() {
    wx.showLoading({
      title: '加载中',
    })
    db.collection('blog').skip(this.data.blogList.length).limit(MAX_LIMIT).orderBy('createTime', 'desc').get().then(res => {
      console.log(res)
      const _blogList = res.data;
      for (let i = 0, len = _blogList.length; i < len; i++) {
        _blogList[i].createTime = _blogList[i].createTime.toString()
      }
      this.setData({
        blogList: this.data.blogList.concat(_blogList)
      })
      wx.hideLoading();
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.getListCloudFn();
    // this.getListMinaFn();
  },
  /**
   * 详情
   */
  goComment(event) {
    wx.navigateTo({
      url: '../blog-comment/blog-comment?blogId=' + event.target.dataset.blogid,
    })
  },
  /**
   * 分享
   */
  onShareAppMessage(event) {
    let blogObj = event.target.dataset.blog;
    return {
      title: blogObj.content, // 标题
      path: `/pages/blog-comment/blog-comment?blogId=${blogObj._id}`, //点击的样式
      // imageUrl:'' // 分享的图片
    }
  }
})